#include "mainwindow.h"

#include "fancytabwidget.h"
#include "qnewprojectdialog.h"
#include "../include/pageinterface.h"
#include "../lib/shared/qproject.h"
#include "../lib/touchRun/touchrun.h"

#include "core.h"

#include <QFileDialog>

#include <QDir>
#include <QXmlStreamWriter>

MainWindow::MainWindow(AbstractCore *core, QWidget *parent):
    QMainWindow(parent),
    m_fancyTab(new FancyTabWidget(core,this)),
    m_core(core),
    m_touchRun(NULL),
    m_isRunning(false)
{
    setCentralWidget(m_fancyTab);

    QAction *ac=m_core->action("New_Action");
    if(ac!=NULL)
    {
        connect(ac,SIGNAL(triggered()),this,SLOT(newProject()));
    }

    ac=m_core->action("Open_Action");
    if(ac!=NULL)
    {
        connect(ac,SIGNAL(triggered()),this,SLOT(openProject()));
    }

    ac=m_core->action("Save_Action");
    if(ac!=NULL)
    {
        connect(ac,SIGNAL(triggered()),this,SLOT(saveProject()));
        ac->setEnabled(false);
    }

    ac=m_core->action("Close_Action");
    if(ac!=NULL)
    {
        connect(ac,SIGNAL(triggered()),this,SLOT(pclose()));
        ac->setEnabled(false);
    }

    ac=m_core->action("Run_Action");
    if(ac!=NULL)
    {
        connect(ac,SIGNAL(triggered()),this,SLOT(run()));
        ac->setEnabled(false);
    }

    connect(QProject::project(),SIGNAL(openProject()),this,SLOT(enableTab()));
    connect(QProject::project(),SIGNAL(durtyChanged()),this,SLOT(updateTitle()));
    connect(QProject::project(),SIGNAL(beforeClosed()),this,SLOT(projectClose()));
    updateTitle();
}

MainWindow::~MainWindow()
{
}

void MainWindow::addTabel(PageInterface *tabel)
{
    int index=0;
    for(index=0;index<m_pages.size();index++)
    {
        if(tabel->index()<m_pages.at(index)->index())
        {
            break;
        }
    }
    m_fancyTab->insertTab(index,tabel->widget(),tabel->icon(),tabel->text(),tabel);
    m_pages.insert(index,tabel);
}

void MainWindow::showIndex(int index)
{
    m_fancyTab->setCurrentIndex(index);
}

void MainWindow::newProject()
{
    QNewProjectDialog dlg(this);

    dlg.exec();
    if(dlg.getRet()==1)
    {
        QString path=dlg.getProPath();
        QString name=dlg.getProName();

        path+=("/"+name);
        QDir dir;
        dir.mkdir(path);

        QProject *p=QProject::project();
        p->newProject(path,name);
    }
}

void MainWindow::enableTab()
{
    for(int i=0;i<m_fancyTab->tagCount();i++)
    {
        m_fancyTab->setTabEnabled(i,true);
    }
    QAction *ac;
    ac=m_core->action("Save_Action");
    if(ac!=NULL)
    {
        ac->setEnabled(true);
    }

    ac=m_core->action("Close_Action");
    if(ac!=NULL)
    {
        ac->setEnabled(true);
    }

    ac=m_core->action("Run_Action");
    if(ac!=NULL)
    {
        ac->setEnabled(true);
    }
}

void MainWindow::projectClose()
{
    for(int i=0;i<m_fancyTab->tagCount();i++)
    {
        m_fancyTab->setTabEnabled(i,m_pages.at(i)->enabled());
    }
    QAction *ac;
    ac=m_core->action("Save_Action");
    if(ac!=NULL)
    {
        ac->setEnabled(false);
    }

    ac=m_core->action("Close_Action");
    if(ac!=NULL)
    {
        ac->setEnabled(false);
    }
    ac=m_core->action("Run_Action");
    if(ac!=NULL)
    {
        ac->setEnabled(false);
    }
}

void MainWindow::openProject()
{
    QString str=QFileDialog::getOpenFileName(this,tr("Open Project"),QDir::currentPath(),tr("Project file (*.pro)"));
    if(str!="")
    {
        QProject::project()->open(str);
    }
}

void MainWindow::updateTitle()
{
    if(QProject::project()->getProjectPath()!="")
    {
        setWindowTitle(QProject::project()->getProjectName()+(QProject::project()->durty()?"*":"")+" -- SoftTouch Project");
    }
    else
    {
        setWindowTitle("SoftTouch Project");
    }
}

void MainWindow::saveProject()
{
    QProject::project()->save();
}

void MainWindow::run()
{
    QAction *ac=m_core->action("Run_Action");
    if(m_touchRun==NULL)
    {
        QProject::project()->save();
        ac->setText("Stop");
        ac->setToolTip("Stop");
        ac->setIcon(QIcon(":/images/stop.png"));
        m_touchRun=new TouchRun(QProject::project()->getProjectPath()+"/"+QProject::project()->getProjectName()+".pro");
        connect(m_touchRun,SIGNAL(Exit()),this,SLOT(stop()));
        m_touchRun->exec();
        m_isRunning=true;
    }
    else
    {
        disconnect(m_touchRun,SIGNAL(Exit()),this,SLOT(stop()));
        delete m_touchRun;
        m_touchRun=NULL;
        ac->setText("Run");
        ac->setToolTip("Run");
        ac->setIcon(QIcon(":/images/run.png"));
        m_isRunning=false;
    }
}

void MainWindow::stop()
{
    delete m_touchRun;
    m_touchRun=NULL;
    QAction *ac=m_core->action("Run_Action");
    ac->setText("Run");
    ac->setToolTip("Run");
    ac->setIcon(QIcon(":/images/run.png"));
    m_isRunning=false;
}

void MainWindow::pclose()
{
    QProject::project()->close();
    updateTitle();
}
